<?php

namespace App\Module;

use App\Models\MainModel;
use App\Dep\MainDep;
use App\Models\ConfigModel;

class MainModule extends BaseModule
{
    public function init()
    {

        // 使用 MainModel 查询对应版本的数据
        $items = MainModel::all();

        // 构建字典结构
        $data['dict'] = $items->map(function ($item) {
            return [
                'id' => $item->id,
                'title' => $item->title,
                'content' => $item->content,
            ];
        });

        $resConfig = ConfigModel::first();
        $data['config'] = [
            'per_second' => $resConfig->per_second,
            'black_second' => $resConfig->black_second,
            'mod' => $resConfig->mod,
            'font_size' => $resConfig->font_size,
            'times' => $resConfig->times,
        ];

        // 返回响应
        return self::response($data);
    }

    public function init1()
    {
        // 使用 MainModel 查询 isUsed 为 1 的数据
        $items = MainModel::where('isUsed', 1)->get();

        // 构建字典结构
        $data['dict'] = $items->map(function ($item) {
            return [
                'id' => $item->id,
                'title' => $item->title,
                'content' => $item->content,
            ];
        });

        $resConfig = ConfigModel::first();
        $data['config'] = [
            'per_second' => $resConfig->per_second,
            'black_second' => $resConfig->black_second,
            'mod' => $resConfig->mod,
            'font_size' => $resConfig->font_size,
            'times' => $resConfig->times,
        ];

        // 返回响应
        return self::response($data);
    }


    public function add($request)
    {
        $param = $request->input();

        $data = [

            'title' => $param['title'],
            'content' => $param['content'],
        ];
        $dep = new MainDep();
        $dep->add($data);

        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();

        $data = [
            'title' => $param['title'],
            'content' => $param['content'],
        ];

        $dep = new MainDep();
        $dep->edit($param['id'], $data);

        return self::response();
    }

    public function isUsed($request)
    {
        $param = $request->input();
        $dep = new MainDep();
        $dep->setIsUsed($param['id']);

        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();
        $id = $param['id'];
        $dep = new MainDep();

        $dep->del($id);
        return self::response();
    }


    public function saveConfig($request){

        $param = $request->input();

        $resConfig = ConfigModel::first();

        ConfigModel::where('id',$resConfig->id)->update(['mod'=>$param['mod'],'font_size'=>$param['font_size'],'per_second' => $param['per_second'],'black_second'=>$param['black_second'],'times'=>$param['times']]);

        return self::response();
    }


}

